package com.vancouver.knows.ui.activity.search;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.Bind;
import butterknife.ButterKnife;

import com.pangu.AMApplication;
import com.pangu.bean.PageInfo;
import com.pangu.service.ActionException;
import com.pangu.task.mark.ATaskMark;
import com.pangu.ui.browser.PullListItemBrowser;
import com.pangu.ui.refresh.ARefreshViewController;
import com.vancouver.knows.MainApplication;
import com.vancouver.knows.R;
import com.vancouver.knows.ui.activity.citydiscount.CityDiscountDetailActivity;
import com.vancouver.knows.ui.activity.citynews.CityNewsDetailActivity;
import com.vancouver.knows.ui.activity.event.EventDetailActivity;
import com.vancouver.knows.business.BusinessModule;
import com.vancouver.knows.business.bean.SearchResultInfo;
import com.vancouver.knows.business.task.mark.SearchResultTaskMark;
import com.vancouver.knows.ui.activity.userpost.UserPostDetailActivity;

@SuppressWarnings("rawtypes")
public class SearchResultListView extends PullListItemBrowser<AMApplication> {
	public View head;
	private Context mContext;
	private BusinessModule bModule;
	private String keyWord;
	private String type;

	public SearchResultListView(Context context, ARefreshViewController mController) {
		super(context, mController);
		mContext = context;
		bModule = ((MainApplication) imContext).getBusinessModule();
	}

	@SuppressWarnings("deprecation")
	@Override
	protected void setListViewParameters(ListView listView) {
		super.setListViewParameters(listView);
		listView.setDivider(new ColorDrawable(getResources().getColor(R.color.line_color)));
		listView.setDividerHeight(1);
	}

	@Override
	public void receiveResult(ATaskMark taskMark, ActionException exception, Object trackerResult) {
		super.receiveResult(taskMark, exception, trackerResult);
		if (exception != null && !exception.getExMessage().equals("")) {
			Toast.makeText(mContext, exception.getExMessage(), Toast.LENGTH_LONG).show();
		}
		if (taskMark instanceof SearchResultTaskMark && taskMark.getTaskStatus() == ATaskMark.HANDLE_OVER)
			notifyDataSetChanged();
	}

	@Override
	protected void addListHeader(ListView listView) {
		super.addListHeader(listView);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		SearchResultInfo searchResultInfo = (SearchResultInfo) parent.getAdapter().getItem(position);
		Class cla = null;
		if (this.type.equals("post")) {
			cla = CityNewsDetailActivity.class;
		} else if (this.type.equals("event")) {
			cla = EventDetailActivity.class;
		} else if (this.type.equals("business")) {
			cla = CityDiscountDetailActivity.class;
		} else if (this.type.equals("userpost")) {
			cla = UserPostDetailActivity.class;
		}
		if (cla != null) {
			Intent intent = new Intent(getContext(), cla);
			intent.putExtra("postId", searchResultInfo.getId());
			if (this.type.equals("userpost")) {
				intent.putExtra(UserPostDetailActivity.POST_ID_EXTRA, searchResultInfo.getId());
			}
			mContext.startActivity(intent);
		}
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
		return false;
	}

	@Override
	protected BaseAdapter createAdapter() {
		return new SearchResultAdapter();
	}

	public void initLoadableView(String keyWord, String type, ATaskMark taskWraper) {
		this.keyWord = keyWord;
		this.type = type;
		super.initLoadableView(taskWraper);
	}

	@Override
	protected void handleLoadNewItems(ATaskMark taskMark) {
		// 发送登录需要的参数
		bModule.getServiceWrapper().userSearchResult(this, (SearchResultTaskMark)taskMark,keyWord, type);
	}

	public class SearchResultAdapter extends BaseAdapter {

		private LayoutInflater lf;

		public SearchResultAdapter() {
			this.lf = LayoutInflater.from(getContext());
		}

		@Override
		public int getCount() {
			return bModule.getCacheManager().getSearchResultCache().getItemInfoCount(mTaskMark);
		}

		@Override
		public Object getItem(int position) {
			return bModule.getCacheManager().getSearchResultCache().getItemInfoByTypeIndex(mTaskMark, position);

		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			ViewValueHand hand = null;
			View view = convertView;
			if (position == getCount() - 1) {
				tryQueryNewItems(true, ATaskMark.TYPE_DEFAULT);
			}
			if (convertView == null) {
				view = lf.inflate(R.layout.search_item, parent, false);
				hand = new ViewValueHand(view);

				view.setTag(hand);
			} else {
				hand = (ViewValueHand) view.getTag();
			}

			SearchResultInfo info = (SearchResultInfo) getItem(position);
			hand.title.setText(info.getPostTitle());
			return view;
		}

		public class ViewValueHand {
			@Bind(R.id.title)
			TextView title;

			public ViewValueHand(View view) {
				ButterKnife.bind(this, view);
			}
		}
	}
}